/**
 * 
 */
package com.sqq.model.kl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sqq.jfinalbase.SqqModel;

/**
 *@author gqy
 *2015-1-13
 */

@TableBind(tableName = "kl_macinfo")
public class KlMacinfoModel extends SqqModel<KlMacinfoModel> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static KlMacinfoModel dao = new KlMacinfoModel();
	
	/**
	 * 更新客户所在商家的客户信息
	 * 到店次数>=3表示老客户，否则表示新客户
	 * 15内天的到场次数>=2表示粉丝客户
	 */
	public static void updateMacInfo(){
		String timeTag = KlParameterModel.getKlMacInfoLastDatetime();
		List<Record> list = KlMacVisitModel.findMacinfo(timeTag);
		for(Record record:list){
			String gwMac = record.getStr("gw_mac");
			String mac = record.getStr("mac");
			String startTime = record.getStr("start_time");
			Record macinfoRecord = Db.findFirst("select * from kl_macinfo where mac=? and gw_mac=? ", mac,gwMac);
			if(macinfoRecord==null){
				int times = KlMacVisitModel.findMacVisitTimes(mac, gwMac);
				Map<String,Object> map = new HashMap<String, Object>();
				map.put("mac", mac);
				map.put("last_time", startTime);
				map.put("create_time", startTime);
				map.put("gw_mac", gwMac);
				if(times>=3){
					map.put("mac_type", "老客户");
				}
				else{
					map.put("mac_type", "新客户");
				}
				boolean isFansMac = KlMacVisitModel.isFansMac(mac, gwMac, timeTag);
				map.put("mac_fans", isFansMac?"1":"");
				Record tookenRecord = RouterTookenModel.getUserRecordByMacAndgwMac(mac, gwMac);
				map.put("uid", tookenRecord.getStr("uid"));
			}
			else{
				String macType = macinfoRecord.getStr("mac_type");
				if(!"老客户".equals(macType)){
					int times = KlMacVisitModel.findMacVisitTimes(mac, gwMac);
					if(times>=3){
						macinfoRecord.set("mac_type", "老客户");
					}
				}
				if(macinfoRecord.getStr("uid")==null){
					Record tookenRecord = RouterTookenModel.getUserRecordByMacAndgwMac(mac, gwMac);
					macinfoRecord.set("uid", tookenRecord.getStr("uid"));
				}
				boolean isFansMac = KlMacVisitModel.isFansMac(mac, gwMac, timeTag);
				macinfoRecord.set("mac_fans", isFansMac?"1":"");
				macinfoRecord.set("last_time", startTime);
			}
		}
	}
	
}
